package controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    // alt + insert
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // req 请求  resp 响应
        req.setCharacterEncoding("UTF-8");
        // 获取参数
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        System.out.println(name+":"+password);
        System.out.println("UserServlet");

        List<Person> personList = new ArrayList<Person>();
        Person person = new Person(1, "lisi", 18);
        personList.add(person);
        Person person1 = new Person(2, "zangsan", 12);
        personList.add(person1);
        req.setAttribute("personList", personList);


        // req 存放值
        req.setAttribute("name",name);
        req.setAttribute("password",password);
        // session
        HttpSession session = req.getSession();
        session.setAttribute("name","lisi");
        // 转发  url 不会变
        req.getRequestDispatcher("user.jsp").forward(req, resp);
        // 重定向
//        resp.sendRedirect("user.jsp");

        // 同一个用户多次请求 在Session 里共享数据
    }
}
